using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Build.Utilities;
using Microsoft.Build.Framework;
using System.Runtime.InteropServices;
using System.Collections.Specialized;
using System.IO;
using System.Reflection;

namespace WMVConvert
{
	public class GetMetadata: Task
	{
		ITaskItem[] videoInput;

		[Required]
		public ITaskItem[] InputVideo
		{
			get
			{
				return videoInput;
			}
			set
			{
				videoInput = value;
			}
		}

		List<ITaskItem> outputVideo;

		[Output]
		public ITaskItem[] OutputVideo
		{
			get
			{
				return outputVideo.ToArray();
			}
			set
			{
				outputVideo.Clear();
				outputVideo.AddRange(value);
			}
		}

		public GetMetadata()
		{
			outputVideo = new List<ITaskItem>();
		}

		public override bool Execute()
		{
			foreach (ITaskItem item in videoInput)
			{
				TaskItem newItem = new TaskItem(item);
				using(MediaInfo mediaInfo=new MediaInfo(newItem.ItemSpec))
				{
					foreach(PropertyInfo prop in typeof(IMediaInfo).GetProperties())
					{
						object value = prop.GetValue(mediaInfo,null);
						newItem.SetMetadata(prop.Name,value.ToString());
					}
				}
				outputVideo.Add(newItem);
			}
			return true;
		}
	}
}
